<?php

namespace Viettrans\FrontBundle\Services;

use Symfony\Component\HttpFoundation\Session;

class MyCaptchaGenerator {
    private $session;
    private $fontSize;
    private $width;
    private $height;
    private $randString;
    private $numChar;
    private $captchaName;
    private $captchaId;
    private $bgColor;
    private $color;
    
    public function __construct(Session $session) {
        $this->session = $session;
        
        # Set default parametters
        $this->fontSize = 10;
        $this->width = 80;
        $this->height = 15;
        $this->numChar = 4;
        $this->captchaName = 'captcha';
        $this->captchaId = 'captcha';
        $this->bgColor = $this->html2rgb('#ffffff');
        $this->color = $this->html2rgb('#ff0000');

        return $this;
    }
    
    public function setFontSize($fontSize) {
        $this->fontSize = $fontSize;
        
        return $this;
    }

    public function setWidth($width) {
        $this->width = $width;
        
        return $this;
    }

    public function setHeight($height) {
        $this->height = $height;
        
        return $this;
    }
    
    public function setNumChar($numChar) {
        $this->numChar = $numChar;
        
        return $this;
    }
    
    public function setCaptchaName($captchaName) {
        $this->captchaName = $captchaName;
        
        return $this;
    }
    
    public function setCaptchaId($captchaId) {
        $this->captchaId = $captchaId;
        
        return $this;
    }

    public function setColor($color) {
        $this->color = $this->html2rgb($color);
        
        return $this;
    }
    
    public function setBgColor($bgColor) {
        $this->bgColor = $this->html2rgb($color);
        
        return $this;
    }
    
    public function render() {
        # Generate random string
        $this->randString = substr(str_shuffle('23456789abcdefghijkmnpqrstuvxyzw'), 0, $this->numChar);
        
        # Save captcha into session
        $this->session->set('captcha', $this->randString);
        
        # Render the image
        $img = imagecreate($this->width, $this->height);
        $bgColor = imagecolorallocate($img, $this->bgColor[0], $this->bgColor[1], $this->bgColor[2]);
        imagefilledrectangle($img, 0, 0,$this->width ,$this->height , $bgColor);
        $color = imagecolorallocate($img, $this->color[0], $this->color[1], $this->color[2]);
        imagestring($img, $this->fontSize, 0, 0, $this->randString, $color);

        ob_start();
        imagejpeg($img);
        $imgData = ob_get_contents();
        ob_end_clean();
        
        imagedestroy($img);
        
        # Encode image to base 64
        $imgBase64Encoded = base64_encode($imgData);
        
        # Return the image tag
        return '<img name="'.$this->captchaName.'" id="'.$this->captchaId.'" src="data:image/jpg;base64,'.$imgBase64Encoded.'" />';
    }
    
    /**
     * Convert color from html to rgb
     * @param string $color Color format #xxxxxx
     * @return array array(red, green, blue)
     */
    private function html2rgb($color) {
        if ($color[0] == '#')
            $color = substr($color, 1);

        if (strlen($color) == 6)
            list($r, $g, $b) = array($color[0].$color[1],
                                     $color[2].$color[3],
                                     $color[4].$color[5]);
        elseif (strlen($color) == 3)
            list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
        else
            return false;

        $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);

        return array($r, $g, $b);
    }
    
    /**
     * Convert color from RGB to html
     * @param int $r Red xx
     * @param int $g Green xx
     * @param int $b Blue xx
     * @return string Color format #xxxxxx 
     */
    function rgb2html($r, $g=-1, $b=-1) {
        if (is_array($r) && sizeof($r) == 3)
            list($r, $g, $b) = $r;

        $r = intval($r); $g = intval($g);
        $b = intval($b);

        $r = dechex($r<0?0:($r>255?255:$r));
        $g = dechex($g<0?0:($g>255?255:$g));
        $b = dechex($b<0?0:($b>255?255:$b));

        $color = (strlen($r) < 2?'0':'').$r;
        $color .= (strlen($g) < 2?'0':'').$g;
        $color .= (strlen($b) < 2?'0':'').$b;
        return '#'.$color;
    }
}
?>
